package per.hx.bs.web.sys.controller;

import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import per.hx.bs.base.BaseController;
import per.hx.bs.base.annotation.BsCLog;
import per.hx.bs.base.domain.Tree;
import per.hx.bs.base.domain.ReturnDTO;
import per.hx.bs.entity.sys.SysMenuDO;
import per.hx.bs.web.sys.service.SysMenuService;

import java.util.List;

/**
 * @author HuXing
 * @date 2018/3/22  9:11
 * <p>
 * 菜单控制
 */
@Controller
@RequestMapping("/sys/menu")
public class MenuController extends BaseController {

    private final String prefix = "system/menu/";

    private final
    SysMenuService sysMenuService;

    @Autowired
    public MenuController(SysMenuService sysMenuService) {
        this.sysMenuService = sysMenuService;
    }

    @RequiresPermissions("sys:menu:menu")
    @GetMapping(value = {"/", ""})
    public String menu() {
        return prefix + "menu";
    }

    @RequiresPermissions("sys:menu:menu")
    @GetMapping(value = "/list")
    @ResponseBody
    public List<SysMenuDO> list(@RequestParam String sort) {
        return sysMenuService.list(sort);
    }

    @BsCLog("进入添加菜单页面")
    @RequiresPermissions("sys:menu:add")
    @GetMapping(value = "/add/{pId}")
    public String addMenu(Model model, @PathVariable("pId") Long pId) {
        model.addAttribute("pId", pId);
        if (pId == 0) {
            model.addAttribute("pName", "根目录");
        } else {
            model.addAttribute("pName", sysMenuService.getOne(pId).getName());
        }
        return prefix + "add";
    }

    @BsCLog("进入编辑菜单页面")
    @RequiresPermissions("sys:menu:edit")
    @GetMapping(value = "/edit/{id}")
    public String edit(Model model, @PathVariable("id") Long id) {
        SysMenuDO sysMenuDO = sysMenuService.getOne(id);
        if (sysMenuDO != null) {
            Long pId = sysMenuDO.getParentId();
            model.addAttribute("pId", pId);
            if (pId == 0) {
                model.addAttribute("pName", "根目录");
            } else {
                model.addAttribute("pName", sysMenuDO.getName());
            }
        } else {
            model.addAttribute("pId", "");
            model.addAttribute("pName", "根目录");

        }
        model.addAttribute("menu", sysMenuDO);

        return prefix + "edit";
    }

    @BsCLog("保存菜单信息")
    @RequiresPermissions("sys:menu:add")
    @PostMapping(value = "/save")
    @ResponseBody
    public ReturnDTO save(SysMenuDO sysMenuDO) {
        sysMenuDO.setCreateId(getUserId());
        if (sysMenuService.save(sysMenuDO) > 0) {
            return ReturnDTO.ok();
        } else {
            return ReturnDTO.error("保存失败");
        }
    }

    @BsCLog("更新菜单信息")
    @RequiresPermissions("sys:menu:edit")
    @PutMapping(value = "/update")
    @ResponseBody
    public ReturnDTO update(SysMenuDO sysMenuDO) {
        sysMenuDO.setUpdateId(getUserId());
        if (sysMenuService.update(sysMenuDO) > 0) {
            return ReturnDTO.ok();
        } else {
            return ReturnDTO.error("更新失败");
        }
    }

    @BsCLog("删除菜单")
    @RequiresPermissions("sys:menu:remove")
    @PostMapping(value = "/remove")
    @ResponseBody
    public ReturnDTO remove(@RequestParam("id") Long id) {
        if (sysMenuService.remove(id) > 0) {
            return ReturnDTO.ok();
        } else {
            return ReturnDTO.error("删除失败");
        }
    }

    @BsCLog("获取菜单信息")
    @GetMapping("/tree")
    @ResponseBody
    public Tree tree() {
        return JSON.parseObject(sysMenuService.getTree(), Tree.class);
    }

    @BsCLog("获取菜单信息By roleId")
    @GetMapping("/tree/{roleId}")
    @ResponseBody
    public Tree tree(@PathVariable("roleId") Long releId) {
        return JSON.parseObject(sysMenuService.getTree(releId), Tree.class);
    }
}
